﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Heyu.Utility
{
    public class MessageBox
    {
        public MessageBox() { }

        /// <summary>
        /// 获取当前页面
        /// </summary>
        /// <returns></returns>
        private static Page getCurrent()
        {
            Page page = (Page)HttpContext.Current.Handler;
            return page;
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        /// <param name="msg">信息</param>
        public static void Show(string msg)
        {
            //ScriptManager  须要引用 System.Web.Extensions
            ScriptManager.RegisterStartupScript(getCurrent(), getCurrent().GetType(), "", "<script>alert('" + msg + "');</script>", false);
        }

        /// <summary>
        /// 添加确认提示框。
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="msg">信息</param>
        public static void AddConfirm(WebControl control, string msg)
        {
            control.Attributes.Add("onclick", "return confirm('" + msg + "?')");
        }


        /// <summary>
        /// 显示对话框并跳转
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转地址</param>
        public static void ShowAndRedirect(string msg, string url)
        {
            ScriptManager.RegisterStartupScript(getCurrent(), getCurrent().GetType(), "", "<script>alert('" + msg + "');location.href='" + url + "';</script>", false);
        }

        /// <summary>
        /// 自定义对话框操作
        /// </summary>
        /// <param name="opeastring">
        /// 自定义内容
        /// 例:alert('你好');
        /// </param>
        public static void ShowCustom(string opeastring)
        {
            ScriptManager.RegisterStartupScript(getCurrent(), getCurrent().GetType(), "", "<script>" + opeastring + "</script>", false);
        }

        /// <summary>
        /// 点击消息对话框的时候后面的页面一直存在
        /// </summary>
        /// <param name="message"></param>
        public static void alert(string message)
        {
            getCurrent().ClientScript.RegisterStartupScript(getCurrent().GetType(), "message", "<script language='javascript' defer>alert('" + message + "');</script>");
        }
        public static void alertAndClose(string message)
        {
            getCurrent().ClientScript.RegisterStartupScript(getCurrent().GetType(), "message", "<script language='javascript' defer>alert('" + message + "');this.close();</script>");
        }
        public static void alertAndClose_lhg(string message, bool refresh = false)
        {
            if (refresh)
            {
                getCurrent().ClientScript.RegisterStartupScript(getCurrent().GetType(), "message", "<script language='javascript' defer>alert('" + message + "');frameElement.api.close();frameElement.api.reload(frameElement.lhgDG);</script>");
            }
            else
            {
                getCurrent().ClientScript.RegisterStartupScript(getCurrent().GetType(), "message", "<script language='javascript' defer>alert('" + message + "');frameElement.api.close();</script>");
            }
            //ScriptManager.RegisterStartupScript(getCurrent(), getCurrent().GetType(), "", "<script>" + opeastring + "</script>", false);
        }

        public static void OpenPage(string url)
        {
            getCurrent().ClientScript.RegisterStartupScript(getCurrent().GetType(), "page", "<script>window.open('" + url + "')</script>", false);
            //ScriptManager.RegisterStartupScript(getCurrent(), getCurrent().GetType(), name, "<script>window.open('" + url + "')</script>", false);
        }



        #region 运政用的弹框OpenWindows
        public static void ShowMessage(string strMsg)
        {
            System.Web.HttpContext.Current.Response.Write("<Script Language='JavaScript'>window.alert('" + strMsg + "');</script>");
        }
        public static void ShowMessage(System.Web.UI.Page page, string strMsg)
        {
            page.Response.Write("<Script Language='JavaScript'>window.alert('" + strMsg + "');</script>");
        }
        public static void ShowMessage(string strMsg, string Url)
        {
            System.Web.HttpContext.Current.Response.Write("<Script Language='JavaScript'>window.alert('" + strMsg + "');window.location.href ='" + Url + "'</script>");
        }
        public static void ShowMessage(System.Web.UI.Page page, string strMsg, string Url)
        {
            page.Response.Write("<Script Language='JavaScript'>window.alert('" + strMsg + "');window.location.href ='" + Url + "'</script>");
        }
        public static void ShowConfirm(string strMsg, string strUrl_Yes, string strUrl_No)
        {
            System.Web.HttpContext.Current.Response.Write("<Script Language='JavaScript'>if ( window.confirm('" + strMsg + "')) {  window.location.href='" + strUrl_Yes +
                              "' } else {window.location.href='" + strUrl_No + "' };</script>");
        }


        public static void showModalDialog(System.Web.UI.Page con, string url)
        {
            con.ClientScript.RegisterStartupScript(con.GetType(), "", url);
        }

        /// <summary>
        /// 弹出新页面，并给textbox赋值，然后执行其他按钮的点击事件
        /// </summary>
        /// <param name="PageName">要执行的页面名</param>
        /// <param name="url">弹出窗口的地址</param>
        /// <param name="weight">页面宽</param>
        /// <param name="heigh">页面高</param>
        /// <param name="txt">返回值接收的控件名</param>
        /// <param name="btn">处理返回值的操作，统一放在同一个button下</param>
        /// <param name="type">用于后台判断执行那个后续操作</param>
        public static void OpenWindow(Page PageName, string url, int width, int heigh, string txt, string btn, string type)
        {
            string js = "<script language='JavaScript'>";
            js += "var result = window.showModalDialog('" + url + "'";
            js += ",''";
            js += ",'dialogWidth=" + width + "px;";
            js += "dialogHeight=" + heigh + "px');";
            js += "if(result != ''&&result != undefined)";
            js += "{window.document.getElementById('" + txt + "').value ='" + type + "'+result;";
            js += "window.document.getElementById('" + btn + "').click();}";
            js += "else";
            js += "{window.document.getElementById('" + txt + "').value = '';}";
            js += "</script>";
            showModalDialog(PageName, js);
        }
        public static void OpenWindow(Page PageName, string url, int width, int heigh, string txt, string btn)
        {
            string js = "<script language='JavaScript'>";
            js += "var result = window.showModalDialog('" + url + "'";
            js += ",''";
            js += ",'dialogWidth=" + width + "px;";
            js += "dialogHeight=" + heigh + "px');";
            js += "if(result != ''&&result != undefined)";
            js += "{window.document.getElementById('" + txt + "').value =result;";
            js += "window.document.getElementById('" + btn + "').click();}";
            js += "else";
            js += "{window.document.getElementById('" + txt + "').value = '';}";
            js += "</script>";
            showModalDialog(PageName, js);
        }
        //public static void OpenWindow(Page PageName, string url, int width, int heigh, string btn)
        //{
        //    string js = "<script language='JavaScript'>";
        //    js += "var result = window.showModalDialog('" + url + "'";
        //    js += ",''";
        //    js += ",'dialogWidth=" + width + "px;";
        //    js += "dialogHeight=" + heigh + "px');";
        //    js += "window.document.getElementById('" + btn + "').click();";
        //    js += "</script>";
        //    showModalDialog(PageName, js);
        //}
        public static void OpenWindow(Page PageName, string url, int width, int heigh, string txt)
        {
            string js = "<script language='JavaScript'>";
            js += "var result = window.showModalDialog('" + url + "'";
            js += ",''";
            js += ",'dialogWidth=" + width + "px;";
            js += "dialogHeight=" + heigh + "px');";
            js += "if(result != ''&&result != undefined)";
            js += "{window.document.getElementById('" + txt + "').value =result;}";
            js += "</script>";
            showModalDialog(PageName, js);
        }
        public static void OpenWindow(Page PageName, string url, int width, int heigh)
        {
            string js = "<script language='JavaScript'>";
            js += "var result = window.showModalDialog('" + url + "'";
            js += ",''";
            js += ",'dialogWidth=" + width + "px;";
            js += "dialogHeight=" + heigh + "px');";
            js += "</script>";
            showModalDialog(PageName, js);
        }

        /// <summary>
        /// 弹出确认对话框，并执行其他按钮的点击事件
        /// </summary>
        /// <param name="PageName"></param>
        /// <param name="btn"></param>
        /// <param name="msg"></param>
        public static void Confirm(Page PageName, string msg, string btn)
        {
            showModalDialog(PageName, "<script language='JavaScript'>var result=confirm('" + msg + "');if(result==false){}else{window.document.getElementById('" + btn + "').click();}</script>");
        }

        /// <summary>
        /// 页面，提示信息，确定按钮，取消按钮

        /// </summary>
        /// <param name="PageName"></param>
        /// <param name="msg"></param>
        /// <param name="btn_ok"></param>
        /// <param name="btn_cancel"></param>
        public static void Confirm(Page PageName, string msg, string btn_ok, string btn_cancel)
        {
            showModalDialog(PageName, "<script language='JavaScript'>var result=confirm('" + msg + "');if(result==true){window.document.getElementById('" + btn_ok + "').click();}else{window.document.getElementById('" + btn_cancel + "').click();}</script>");
        }
        #endregion


    }

}
